<?php
namespace App\Service\Item\Fxotary\Admin;

use App\Service\Item\Fxotary\BaseService as Service;
use Illuminate\Support\Facades\DB;

class YewuService extends Service
{
    //业务

    /**
     * 列表
     * @param int $menu_id 菜单ID
     */
    public function getIndex($menu_id)
    {
        $db = $this->dbConn($this->xmData);
        if ($menu_id) {
            $db = $db->where('menu_id', $menu_id);
        }
        $total = $db->count();
        $res = $db->select('id', 'field_cn as title', 'field_en as key', 'field_val as val', 'field_type as ftype', 'type')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        return $this->toback(0, '', $res, $total);
    }

    //后台功能一个
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->xmData)
            ->select('id', 'field_cn as title', 'field_en as key', 'field_val as val', 'field_type as ftype')
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        return $this->toback(0, '', $res);
    }

    /**
     * 保存更新
     * @param int $id
     * @param string $val
     */
    public function modify($id, $val)
    {
        $res = (array) $this->dbConn($this->xmData)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $ftype = $res['field_type'];
        if ($ftype == 3) {
        }
        $arys = [
            'field_val' => $val,
            'uptime' => toDate(),
        ];
        $up = $this->dbConn($this->xmData)->where('id', $id)->update($arys);
        if (!$up) {
            return $this->toback(-2, 'error');
        }
        return $this->toback();
    }
}